package com.dkd.manage.service.impl;

import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.dto.ChannelConfigDto;
import com.dkd.manage.domain.dto.ChannelSkuDto;
import com.dkd.manage.domain.vo.ChannelVo;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.service.IChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 售货机货道Service业务层处理
 *
 * @author alex
 * @date 2025-03-27
 */
@Slf4j
@Service
public class ChannelServiceImpl implements IChannelService {
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询售货机货道
     *
     * @param id 售货机货道主键
     * @return 售货机货道
     */
    @Override
    public Channel selectChannelById(Long id) {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询售货机货道列表
     *
     * @param channel 售货机货道
     * @return 售货机货道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel) {
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增售货机货道
     *
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel) {
        channel.setCreateTime(DateUtils.getNowDate());
        return channelMapper.insertChannel(channel);
    }

    /**
     * 修改售货机货道
     *
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel) {
        channel.setUpdateTime(DateUtils.getNowDate());
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除售货机货道
     *
     * @param ids 需要删除的售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids) {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除售货机货道信息
     *
     * @param id 售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id) {
        return channelMapper.deleteChannelById(id);
    }

    /**
     * 批量插入售货机货道
     *
     * @param list
     * @return 结果
     */
    public int batchInsertChannels(List<Channel> list) {
        return channelMapper.batchInsertChannels(list);
    }

    /**
     * 根据商品id统计关联的货道信息
     *
     * @param skuIds
     * @return 统计结果
     */
    public int countChannelBySkuIds(Long[] skuIds) {
        return channelMapper.countChannelBySkuIds(skuIds);
    }

    /**
     * 根据售货机软编号查询货道信息
     *
     * @param innerCode
     * @return 结果
     */
    public List<ChannelVo> listByInnerCode(String innerCode) {
        return channelMapper.selectChannelListByInnerCode(innerCode);
    }

    /**
     * 根据售货机软编号和货道编号修改货道关联产品信息
     *
     * @param channelConfigDto
     * @return
     */
    @Transactional
    public int updateChannelByChannelCodeAndInnerCode(ChannelConfigDto channelConfigDto) {
        //获取前端传递过来的集合
        List<ChannelSkuDto> channelSkuDtoList = channelConfigDto.getChannelList();
        //根据前端传递过来的参数查询售货机货道信息
        List<Channel> collect = channelSkuDtoList.stream().map(item ->
                {
                    String channelCode = item.getChannelCode();
                    String innerCode = item.getInnerCode();
                    Channel channel = channelMapper.selectChannelByInnerCodeAndChannelCode(innerCode, channelCode);
                    if (channel != null) {
                        //更新关联的商品id
                        channel.setSkuId(item.getSkuId());
                        //更新修改时间
                        channel.setUpdateTime(DateUtils.getNowDate());
                    }
                    return channel;
                }
        ).collect(Collectors.toList());
        log.info("查询到的售货机货道信息：{}", collect);
        return channelMapper.batchUpdateByInnerCodeAndChannelCode(collect);
    }
}
